// page/sign/sign.js
Page({
  data: {
    isHide: false,
    sex: "",
    region: ["福建省", "厦门市", "湖里区"],
    // course: [ "科目二", "科目三", "科目四"],
    index: 0,
    username: '',
    gender: "",
    phone: "",
    sponsor: "",
    region: "",
    edu: 2,
    picker_arr: [],
    picker_index: 0,
    picker_arrd: [],
    picker_indexd: 0,
    isSub: 1
  },
  onLoad: function (options) {
    var that = this
    that.setData({
      isHide: true,
      options: options
    })
  },
  // next:function(e){
  //   var that = this;
  //   wx.navigateTo({
  //     url: app.globalData.host + '&g=App&m=Index&a=feedback'+that.data.username+that.data.sex,
  //   })
  // },
  Change: function (e) {
    this.setData({
      picker_index: e.detail.value,
    })
    // console.log(this.data.courseItem[this.data.picker_index].id)
  },
  Changed: function (e) {
    this.setData({
      picker_indexd: e.detail.value,
    })
    // console.log(this.data.picker_index)
  },
  onShow: function () {
    var app = getApp()
    var that = this;
    var picker_arr = []
    var picker_arrd = []
    var uid = wx.getStorageSync('uid');
    wx.getStorage({
      key: 'sex',
      success: function (res) {
        that.setData({
          sex: res.data,
        })
      },
    })
    wx.request({
      url: app.globalData.host + '&g=App&m=Index&a=feedback',
      data: {
        uid: uid,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        var courseItem = res.data.item

        var stat = res.data.store
        var scene = res.data.scene
        stat.forEach(function (e) {
          picker_arrd.push(e.title);
        })
        courseItem.forEach(function (e) {
          picker_arr.push(e.title);
        })
        that.setData({
          courseItem: res.data.item,
          storeItem: res.data.store,
          picker_arr: picker_arr,
          picker_arrd: picker_arrd,
          scene: res.data.scene
        })
      }
    })
  },
  username: function (e) {
    this.setData({
      //  username: e.detail.value
    })
    // console.log(e.detail.value)
  },
  gender: function (e) {
    this.setData({
      // gender: e.detail.value
    })
  },
  tpl: function (e) {
    this.setData({
      // phone: e.detail.value
    })
  },
  sponsor: function (e) {
    this.setData({
      sponsor: e.detail.value
    })
  },
  region: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  // loginBtnClick:function(e){
  //   console.log(this.data.name + this.data.sex + this.data.phone);
  // },
  // formSubmit: function (e) {
  //   console.log(e.detail.value);
  //   // var formData = e.detail.value;
  //   var phone = e.detail.value.tpl;
  //   var name = e.detail.value.username;
  //   var sex = e.detail.value.sex;
  //   var msg = e.detail.value.msg;
  //   var address = e.detail.value.address;
  //   var intro = e.detail.value.intro;
  //   var edu = e.detail.value.edu
  //   wx.request({
  //     url: app.globalData.host + '&g=App&m=Index&a=feedback',
  //     data: {
  //       'name': name,
  //       'uid': uid,
  //       'sex': sex,
  //       'intro': intro,
  //       'msg': msg,
  //       'phone': tpl,
  //       'address': addres,
  //       'edu': edu
  //     },
  //     header: {
  //       'Content-Type': 'application/json'
  //     },
  //     success: function (res) {
  //       console.log(res.data)
  //       that.modalTap();
  //     }
  //   })
  // },  
  formSubmit: function (e) {
    var phone = e.detail.value.phone;
    var username = e.detail.value.username;
    //var sex = this.data.sex;
    var msg = e.detail.value.msg;
    //var address = this.data.address;
    var intro = e.detail.value.intro;
    // var edu = this.data.edu;
    var app = getApp();
    var item_id = this.data.courseItem[this.data.picker_index].id
    var store = this.data.storeItem[this.data.picker_indexd].id
    const that = this;
    var uid = wx.getStorageSync('uid');
    // console.log(this.data.getApp())
    // console.log(this.data.username + this.data.username.phone)

    if (username.length < 1 || phone.length < 11) {
      wx.showModal({
        title: '提示',
        content: '请填写名称和电话',
        showCancel: false
      })
    } else {
      wx.showLoading()
      if (that.data.isSub == 0) {
        return false
      }
      that.setData({
        isSub: 0,
      })
      wx.request({
        url: app.globalData.host + '&g=App&m=Index&a=feedbackSub',
        data: {
          name: this.data.username,
          uid: uid,
          item_id: item_id,
          phone: phone,
          store: store,
          intro: intro,
          msg: msg,
        },
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          wx.hideLoading()
          if (res.data.status == 1) {
            wx.showToast({
              title: '报名成功',
              icon: 'loading',
              duration: 2000,
              complete: function (res1) {
                //page/mall/comfirm_course
                wx.navigateTo({
                  url: '/page/mall/comfirm_course?id=' + item_id + '&item_id=' + res.data.add_id
                })
              }
            })
            return false;
          } else {            
            wx.showToast({
              title: '报名失败',
              icon: 'loading',
              duration: 2000,
              complete: function (res) {
                setTimeout(function () {
                  that.setData({
                    isSub: 1,
                  })
                }, 2000)
              }
            })
            return false;
          }
        }
      })

    }
  },
  // 分享
  onShareAppMessage: function (res) {
    var uid = wx.getStorageSync('uid');
    var that = this;
    var app = getApp()
    var scene = that.data.scene
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '自定义转发标题',
      path: 'page/sign/sign?scene=scene',
      success: function (res) {
        // 转发成功
        wx.request({
          url: app.globalData.host + '&g=App&m=Index&a=feedback',
          data: {
            scene: scene
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res.data)
            that.setData({
            })
          }
        })
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})